package com.sql.parser.lineage;

import com.sql.parser.lineage.pg.PgCreateIndexTranslateListener;
import mysql.MySqlLexer;
import mysql.MySqlParser;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.TokenStreamRewriter;
import org.antlr.v4.runtime.tree.ParseTreeWalker;

public class PgTranslate {
    public String translate(String sql) {
        CharStream charStream = CharStreams.fromString(sql);
        MySqlLexer mySqlLexer = new MySqlLexer(charStream);
        CommonTokenStream commonTokenStream = new CommonTokenStream(mySqlLexer);
        TokenStreamRewriter rewriter = new TokenStreamRewriter(commonTokenStream);

        MySqlParser parser = new MySqlParser(commonTokenStream);
        MySqlParser.RootContext root = parser.root();

        PgCreateIndexTranslateListener createIndexTranslateListener = new PgCreateIndexTranslateListener(rewriter);
        ParseTreeWalker createIndexParseTreeWalker = new ParseTreeWalker();
        createIndexParseTreeWalker.walk(createIndexTranslateListener, root);

        return rewriter.getText();
    }
}
